/**
 *
 */

package com.moogene.saad.gameplay.stages;

import java.util.ArrayList;
import java.util.HashMap;

import com.moogene.android.InfoMediator;
import com.moogene.android.engine.mmi.Book;
import com.moogene.android.engine.mmi.Layer;
import com.moogene.android.engine.mmi.LayerCacher;
import com.moogene.android.engine.mmi.widget.Button;
import com.moogene.android.engine.mmi.widget.Widget;
import com.moogene.android.engine.mmi.widget.Widget.OnClickListener;
import com.moogene.saad.gameplay.attacter.Attacker;
import com.moogene.saad.gameplay.defender.TowerTemplate;

/**
 * @author 28849218
 */
public class DefendStage {
	private final static String TXT_BACK_BTN = "Back";

	private final static int HEIGHT_SELECTOR_BUTTON = 50;

	private static DefendStage sBuildStage;

	private BattleField sCurBattleField;

	private DefendStage() {
		
	}

	public static DefendStage getInstance() {
		if (sBuildStage == null) {
			sBuildStage = new DefendStage();
		}
		return sBuildStage;
	}

	public void start(ArrayList<Attacker> attackers) {
//		sCurBattleField = BattleFieldsManger.getInstance().getLastBattleField();
		Book.getInstance().gotoPage("stages.BattleField","level1");
		sCurBattleField = (BattleField) Book.getInstance().getCurrentPage();
		sCurBattleField.clear();
		sCurBattleField.addAttackers(attackers);
		addHuds();
	}
	
	public BattleField getBattleField(){
		return sCurBattleField;
	}

	private void addHuds() {
		HashMap<String, TowerTemplate> towerTemplates = sCurBattleField
				.getTowerTemplates();
//		Layer towerSelector = new Layer(InfoMediator.getScreenWidth()
//				- WIDTH_TOWER_SELECTOR, 0, WIDTH_TOWER_SELECTOR,
//				1000, Layer.HUD) {
//			
//		};
		TowerSelector towerSelector = (TowerSelector) LayerCacher.getLayer("stages.TowerSelector");
		towerSelector.fill(towerTemplates.values());
		towerSelector.link(sCurBattleField);
//		Layout layout = new GirdLayout(-1, 2,
//				HEIGHT_SELECTOR_ITEM);
//		towerSelector.setLayout(layout);
//		for (TowerTemplate t : towerTemplates.values()) {
//			towerSelector.addLayer(t);
//		}
		Book.getInstance().addLayer(towerSelector);
		Button backBtn = new Button(20,
				InfoMediator.getScreenHeight() - 20 - HEIGHT_SELECTOR_BUTTON, 160, HEIGHT_SELECTOR_BUTTON);
		backBtn.setText(TXT_BACK_BTN);
		backBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(Widget v) {
				Book.getInstance().gotoPage("menus.MainMenu");
			}
		});
		backBtn.setPriority(Layer.HUD);
		Book.getInstance().addLayer(backBtn);
	}

}
